package com.coder520.workflow.service;

import com.coder520.attend.dao.AttendMapper;
import com.coder520.attend.entity.Attend;
import com.coder520.workflow.dao.ReAttendMapper;
import com.coder520.workflow.entity.ReAttend;
import org.activiti.engine.HistoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017-7-14.
 */
@Service
public class ReAttendServiceImp implements ReAttendService {

    /**
     * 任务关联补签BMPN
     */
    private static final String RE_ATTEND_SIGN = "re_attend";
    private static final String RE_ATTEND_FLOW_ID = "re_attend";
    /**
     * 流程下一步处理人
     */
    private static final String NEXT_HANDLER = "next_handler";
    /**
     * 补签处理状态
     */
    private static final Byte RE_ATTEND_STATUS_ONGOING = 0;
    private static final Byte RE_ATTEND_STATUS_PASS = 1;
    private static final Byte RE_ATTEND_STATUS_REFUSE = 2;


    @Autowired
    private RuntimeService runtimeService;

    @Autowired
    private TaskService taskService;

    @Autowired
    private HistoryService historyService;

    @Autowired
    private ReAttendMapper reAttendMapper;

    @Autowired
    private AttendMapper attendMapper;

    @Transactional
    @Override
    public void startAttendFlow(ReAttend reAttend) {
        //从公司组织架构中 查询到此人上级领导用户名  (这里用写死的方式测试)
        reAttend.setCurrentHandler("laozhao123");
        reAttend.setStatus(RE_ATTEND_STATUS_ONGOING);
        //插入数据库补签表中
        reAttendMapper.insertSelective(reAttend);
        Map<String,Object> map = new HashMap<String, Object>();
        map.put(RE_ATTEND_SIGN,reAttend);
        map.put(NEXT_HANDLER,reAttend.getCurrentHandler());
        //启动补签流程实例
        ProcessInstance instance = runtimeService.startProcessInstanceByKey(RE_ATTEND_FLOW_ID,map);
        //提交用户补签任务
        Task task = taskService.createTaskQuery().processInstanceId(instance.getId()).singleResult();
        taskService.complete(task.getId(),map);

//        //开启一个工作流任务
//        ProcessInstance instance = runtimeService.startProcessInstanceByKey(RE_ATTEND_FLOW_ID,variables);
//        System.out.println(instance.getId() + "------" + instance.getActivityId());
//        Task task = taskService.createTaskQuery().processInstanceId(instance.getId()).singleResult();
//        System.out.println(task.getId() + "-----" + task.getName());
//        Map<String,Object> map = new HashMap<String, Object>();
//        map.put("attend_morning","09:00");
//        map.put("attend_evening","18:30");
//        //一个节点完成complete,往下个节点传数据map
//        taskService.complete(task.getId(),map);
    }

    @Override
    public List<ReAttend> listTasks(String userName) {
        List<ReAttend> reAttendList = new ArrayList<ReAttend>();
        List<Task> taskList = taskService.createTaskQuery().processVariableValueEquals(userName).list();
        //转换成页面实体
        if(!taskList.isEmpty()) {
            for (Task task:taskList) {
                Map<String,Object> variable = taskService.getVariables(task.getId());
                ReAttend reAttend = (ReAttend) variable.get(RE_ATTEND_SIGN);
                reAttend.setTaskId(task.getId());
                reAttendList.add(reAttend);
            }
        }
        return reAttendList;

//        List<Task> taskList = taskService.createTaskQuery().taskDefinitionKey("re_attend_approve").active().list();
//        Map<String,Object> map = taskService.getVariables(taskList.get(0).getId());
//        return taskList;
    }

    @Override
    public List<ReAttend> reAttendList(String userName) {
        List<ReAttend> reAttendList = reAttendMapper.selectByUserName(userName);
        return reAttendList;
    }

    @Transactional
    @Override
    public void approve(ReAttend reAttend) {//String taskId
        Task task = taskService.createTaskQuery().taskId(reAttend.getTaskId()).singleResult();
        if(RE_ATTEND_STATUS_PASS.toString().equals(reAttend.getApproveFlag())) {
            //审批通知 修改补签数据状态和原表签到状态
            reAttend.setStatus(RE_ATTEND_STATUS_PASS);
            reAttendMapper.updateByPrimaryKeySelective(reAttend);

            ReAttend reAttend2 = reAttendMapper.selectByPrimaryKey(reAttend.getId());
            Attend attend = new Attend();
            attend.setId(reAttend2.getAttendId());
            //attend.setUserId(1L);
            attend.setAttendMorning(reAttend2.getReAttendMorn());
            attend.setAttendEvening(reAttend2.getReAttendEven());
            attend.setAttendStatus(RE_ATTEND_STATUS_PASS);
            attend.setAbsence(0);

            attendMapper.updateByPrimaryKeySelective(attend);
//            attend.set


        }
        else if(RE_ATTEND_STATUS_REFUSE.toString().equals(reAttend.getApproveFlag())) {
            reAttend.setStatus(RE_ATTEND_STATUS_REFUSE);
            reAttendMapper.updateByPrimaryKeySelective(reAttend);
        }
        taskService.complete(reAttend.getTaskId());
//        Task task = taskService.createTaskQuery().taskId(taskId).singleResult();
//        taskService.complete(taskId);
    }

}
